using UnityEngine;
using UnityEngine.UI;

public class ParrySkillController : Skill
{
    [Header("Parry")] [SerializeField] private UiSkillTreeSlot parryUnlockButton;
    public bool ParryUnlocked { get; private set; }

    [Header("Parry restore")] [SerializeField]
    private UiSkillTreeSlot restoreUnlockButton;

    [Range(0f, 1f)] [SerializeField] private float restoreHpAmount;
    private bool restoreUnlocked;

    [Header("Parry with mirage")] [SerializeField]
    private UiSkillTreeSlot parryWithMirageUnlockButton;

    private bool parryWithMirageUnlocked;

    protected override void Start()
    {
        base.Start();
        parryUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockParry);
        restoreUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockParryRestore);
        parryWithMirageUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockParryWithMirage);
    }

    protected override void CheckUnlock()
    {
        UnlockParry();
        UnlockParryRestore();
        UnlockParryWithMirage();
    }

    public override void Use()
    {
        base.Use();
        if (!restoreUnlocked) return;
        int restoreAmount = Mathf.RoundToInt(Player.Stats.GetMaxHp() * restoreHpAmount);
        Player.Stats.IncreaseHpBy(restoreAmount);
    }

    private void UnlockParry()
    {
        if (!parryUnlockButton.unlocked || ParryUnlocked) return;
        ParryUnlocked = true;
    }

    private void UnlockParryRestore()
    {
        if (!restoreUnlockButton.unlocked || restoreUnlocked) return;
        restoreUnlocked = true;
    }

    private void UnlockParryWithMirage()
    {
        if (!parryWithMirageUnlockButton.unlocked || parryWithMirageUnlocked) return;
        parryWithMirageUnlocked = true;
    }

    public void MakeMirageOnParry(Transform respawnTransform)
    {
        if (parryWithMirageUnlocked)
            SkillManager.Clone.CreateCloneWithDelay(respawnTransform);
    }
}